/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actor.weapons;

import Actor.Actor;
import Actor.weapons.Bullet;

/**
 *
 * @author Eric-Standard
 */
public class Shotgun extends Weapon {

    private double spread;
    private static final double DEFAULT_SPREAD = .02;
    private static final int SHOTS = 8;
    private static final int DEFAULT_CLIPSIZE = 8;
    private static final int DEFAULT_DAMAGE = 6;

    public Shotgun() {
        super();
        spread = DEFAULT_SPREAD;
        this.clipSize = DEFAULT_CLIPSIZE;
        this.ammo = 8;
        this.maxAmmo = 400;
        this.reloadTimeout = 500;
        this.cooldownTimeout = 1000 / 2;
        this.damage = DEFAULT_DAMAGE;
    }

    public Shotgun(Actor owner) {
        super(owner);
        spread = DEFAULT_SPREAD;
        this.clipSize = DEFAULT_CLIPSIZE;
        this.maxAmmo = 400;
        this.reloadTimeout = 500;
        this.cooldownTimeout = 400;
        this.damage = DEFAULT_DAMAGE;
    }

    @Override
    public void act(long dt) {
        this.angle = owner.getAngle();
        this.pos = owner.getPos();
        //Update cooldown and reload times
        if (firing) {
            cooldownTime -= dt;
            if (cooldownTime < 0) {
                cooldownTime = 0;
                firing = false;
            }
        }
        if (reloading) {
            reloadTime -= dt;
            if (reloadTime < 0) {
                reloadTime = 0;
                //Reload one shell
                if (ammo < clipSize && totalAmmo != 0) {
                    ammo++;
                    totalAmmo--;
                }
                if (ammo < clipSize && totalAmmo != 0) {
                    reloadTime = reloadTimeout;
                } else {
                    reloading = false;
                }
            }
        }
    }

    @Override
    public boolean fire() {
        if (firing) {
            return false;
        }
        if (ammo == 0) {
            reload();
            return false;
        }
        if (reloading) {
            reloading = false;
        }
        ammo--;
        for (int i = 0; i < SHOTS; i++) {
            Bullet bullet = new Bullet();
            bullet.setOwner(owner);
            bullet.setPos(pos);
            bullet.setDamage(damage);
            double adjustedAngle = (world.rand.nextGaussian() - .5) * spread + angle;
            bullet.setAngle(adjustedAngle);
            world.add(bullet);
        }
        cooldownTime = cooldownTimeout;
        firing = true;
        return true;
    }

    @Override
    public void reload() {
        super.reload();
        if (firing || reloading) {
            return;
        }
        if (ammo < clipSize && totalAmmo != 0) {
            reloadTime = reloadTimeout;
            reloading = true;
        }
    }
}
